<?php
require_once('internal/IDatabase.interface.php');
/*
mae. 1.0
MySQL database support
*/

class Database implements IDatabase {
	public $affected_rows, $num_rows;

	function __construct($server, $user, $pass, $database) {
		$this->sql = @mysql_connect($server, $user, $pass);
		$this->db = @mysql_select_db($database);
		if (!$this->db || !$this->sql) error('db_con');
	}

	function __destruct() {
		return mysql_close($this->sql);
	}

	public function query($que) {
		$q = mysql_query($que);
		if (strpos(strtolower($que), 'select') === 0) $this->num_rows = mysql_num_rows($q);
		$this->affected_rows = mysql_affected_rows($this->sql);
		return $q;
	}

	public function fetch_assoc($res) {
		return mysql_fetch_assoc($res);
	}

	public function error() {
		return mysql_error();
	}

	public function fetch_row($res) {
		return mysql_fetch_row($res);
	}

	public function fetch_array($res) {
		return mysql_fetch_array($res);
	}

	public function select($database, $what='*', $where=array()) {
		global $mae;
		$wheres = (count($where)==0) ? '' : ' WHERE '.implode(', ', $where);
		return $this->query("SELECT $what FROM {$mae[db_prefix]}$database$wheres;");
	}
}

?>
